﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Tasker.Common;
using Tasker.Composition.Events;
using Tasker.Infrastructure.Entities;
using Tasker.Infrastructure.Repositories;
using Tasker.Infrastructure.Services;
using Tasker.Composition;

namespace Tasker.Services
{
    public class AccountService : IAccountService
    {
        #region Private fields

        private readonly IUsersRepository _users = Resolver.Resolve<IUsersRepository>();
        private readonly IUserEmailsRepository _emails = Resolver.Resolve<IUserEmailsRepository>();
        private readonly IUserPhonesRepository _phones = Resolver.Resolve<IUserPhonesRepository>();

        #endregion

        public IUser SelectUser(string nick)
        {
            return _users.SelectByLogin(nick);
        }

        public IUser SelectUser(int id)
        {
            return _users.SelectById(id);
        }

        public IEnumerable<IUser> SelectAllUsers()
        {
            return _users.SelectAll();
        }

        public IEnumerable<string> GetUserEmails(int userId)
        {
            return from userEmail in _emails.SelectByUserId(userId)
                   select userEmail.Email;
        }

        public IEnumerable<string> GetUserPhones(int userId)
        {
            return from userPhone in _phones.SelectByUserId(userId)
                   select userPhone.Phone;
        }

        public void DeleteAllEmails(int userId)
        {
            _emails.DeleteAll(userId);
        }

        public void DeleteAllPhones(int userId)
        {
            _phones.DeleteAll(userId);
        }

        public void CreateEmail(int userId, string email)
        {
            IUserEmail userEmail = _emails.Create();
            userEmail.Email = email;
            userEmail.UserId = userId;
            _emails.Insert(userEmail);
        }

        public void CreatePhone(int userId, string phone)
        {
            IUserPhone userPhone = _phones.Create();
            userPhone.Phone = phone;
            userPhone.UserId = userId;
            _phones.Insert(userPhone);
        }

        public bool ValidateUser(string login, string password)
        {
            var user = _users.SelectByLogin(login);
            return user != null && user.PasswordHash == password.ComputeMD5();
        }

        public IUser Register(string login, string position, string password)
        {
            var user = _users.Create();
            user.Login = login;
            user.Position = position;
            user.PasswordHash = password.ComputeMD5();

            _users.Insert(user);

            /*if (!_errorAggregator.HasErrors)
            {
                return user;
            }*/
            
            //return null;
            return user;
        }

        public bool isUserExist(string login)
        {
            return _users.SelectByLogin(login) != null;
        }

        public void UpdateUser(IUser user)
        {
            _users.Update(user);
        }

        public IUser SignedUser
        {
            get
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    var user = _users.SelectByLogin(HttpContext.Current.User.Identity.Name);
                    return user;
                }
                return null;
            }
        }
    }
}
